package logic

import (
	"context"
	"errors"
	"fmt"
	"os"
	"videoProject/common/checkx"

	"videoProject/src/filemanage/rpc/file"
	"videoProject/src/filemanage/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DownloadFileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDownloadFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DownloadFileLogic {
	return &DownloadFileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DownloadFileLogic) DownloadFile(in *file.DownloadFileRequest) (*file.DownloadFileResponse, error) {
	// todo: add your logic here and delete this line
	logx.Info("DownloadFile............")
	if checkx.PathExists(l.svcCtx.Config.Path, true) == false {
		l.Logger.Error("DownloadFile file directory failed")
		return nil, errors.New(fmt.Sprintf("DownloadFile file directory failed"))
	}
	path := l.svcCtx.Config.Path + in.Name
	content, err := os.ReadFile(path)
	if err != nil {
		return &file.DownloadFileResponse{
			Message: err.Error(),
		}, err
	}

	return &file.DownloadFileResponse{
		Content: content,
		Message: in.Name,
	}, nil
}
